{% extends 'web/layout/basic.html' %}
{% load static %}
{% block title %}
用户短信登录
{% endblock %}
{% block css %}
    <link rel="stylesheet" href="{% static 'css/account.css' %}">
    <style>
        .error-msg {
            color: red;
            position: absolute;
            font-size: 13px;
        }
    </style>
{% endblock %}
{% block content %}
    <div class="account">
        <div class="title">用户短信登录</div>
        <form id="regForm" method="POST" novalidate>
            {% csrf_token %}
            {% for field in form %}
                {% if field.name == 'code' %}
                    <div class="form-group">
                        <label for="{{ field.id_for_label }}">{{ field.label }}</label>
                        <div class="row">
                            <div class="col-xs-7">
                                {{ field }}
                                <span class="error-msg"></span>
                            </div>
                            <div class="col-xs-5">
                                <input id="btnSms" type="button" class="btn btn-default"  value="点击获取验证码">
                            </div>
                        </div>
                    </div>
                {% else %}
                    <div class="form-group">
                        <label for="{{ field.id_for_label }}">{{ field.label }}</label>
                        {{ field }}
                        <span class="error-msg"></span>
                    </div>
                {% endif %}
            {% endfor %}
             <div>
                 <div class="sms" style="float:right;border: 1px solid beige">
                    <a href="{% url 'login' %}"> 用户账号密码登录</a>
                </div>
            </div>
            <div class="row">
                <div class="col-xs-3">
                    <input id="btnSubmit" type="button" class="btn btn-primary" value="登 录"/>
                </div>
            </div>
        </form>
    </div>
{% endblock %}

{% block js %}
    <script>
        $(function () {
            bindClick();
            bindBtncommit();
        });
        function bindClick() {
            $("#btnSms").click(function () {
                $(".error-msg").empty();
                var mobilePhone = $("#id_mobile_phone").val();
                $.ajax({
                    url:"{% url 'send_sms' %}",
                    data:{mobile_phone: mobilePhone, tpl: "login"},
                    type: "GET",
                    dataType: "JSON",
                    success:function (res) {
                        if(res.status){
                            // 注册无错误，获取验证码并倒计时
                            sendSmsremid()
                        }else{
                            //console.log(1);
                            //console.log(res.error);
                            $.each(res.error, function (key,value) {
                                $("#id_"+ key).next().text(value[0])
                            })
                        }
                    },
                });
                // 倒计时并禁止重复发送短信

            })
        }
        function sendSmsremid() {
                    console.log(1);
                    var $btnsms = $("#btnSms");
                    $btnsms.prop("disabled", true);
                    var time=60;
                    var obj = setInterval(function () {
                        time -=1;
                        $btnsms.val(time+"秒重新发送");
                        if(time<1){
                            clearInterval(obj);
                            $btnsms.val("点击获取验证码").prop("disabled", false)
                        }
                    }, 1000)

                }
        // 给注册按钮绑定提交功能（ajax—）
        function bindBtncommit() {
            $("#btnSubmit").click(function () {
                $(".error-msg").empty();
                $.ajax({
                    url: "{% url "login_sms" %}",
                    data: $("#regForm").serialize(), //获取所有form下的输入框加CSRF验证
                    dataType: "JSON",
                    type: "POST",
                    success:function (res) {
                        //console.log(res)
                        if(res.status){
                            location.href = res.data;
                        }else {
                            $.each(res.error, function (key,value) {
                                $("#id_"+ key).next().text(value)
                            })
                        }

                    }

                })
            })
        }
    </script>
{% endblock %}